package com.example.sunlight.unicomsensing.View;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.unicomsensing.R;

/**
 * Created by SunLight on 2017/9/15.
 */

public class CarSuperView extends View {
    Resources resources;
    Bitmap one, two, three, one_sha, one_sha2;
    ShaderThread shaderThread;
    MoveThread moveThread;
    JanThread janThread;
    SizeChange sizeChange;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    CarSuperView.this.invalidate();
                    break;
            }
        }
    };

    public CarSuperView(Context context) {
        super(context);
        init();
    }

    public void init() {
        resources = getResources();
        one = ((BitmapDrawable) resources.getDrawable(R.drawable.blue_car)).getBitmap();
        two = ((BitmapDrawable) resources.getDrawable(R.drawable.green_caar)).getBitmap();

        shaderThread = new ShaderThread();
        shaderThread.start();        three = ((BitmapDrawable) resources.getDrawable(R.drawable.tou)).getBitmap();

        moveThread = new MoveThread();
        moveThread.start();
        janThread = new JanThread();
        janThread.start();
        sizeChange = new SizeChange();
        sizeChange.start();

        Matrix matrix = new Matrix();
// 向左旋转45度，参数为正则向右旋转  
        matrix.postRotate(-10);
//bmp.getWidth(), 500分别表示重绘后的位图宽高  
        one_sha = Bitmap.createBitmap(one, 0, 0, one.getWidth(), one.getHeight(), matrix, true);
        matrix.postRotate(10);
        one_sha2 = Bitmap.createBitmap(one, 0, 0, one.getWidth(), one.getHeight(), matrix, true);
        kit1 = Color.argb(200, 255, 0, 12);
        kit2 = Color.argb(100, 255, 0, 12);
    }

    public CarSuperView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CarSuperView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    int kit = Color.argb(85, 255, 255, 255);


    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(kit);
        paint.setAntiAlias(true);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        paint.setStrokeWidth(7);
        paint.setPathEffect(new DashPathEffect(new float[]{50, 20}, num));
        canvas.drawLine(0, 240, getWidth(), 240, paint);
        canvas.drawLine(0, 390, getWidth(), 390, paint);
        canvas.drawLine(0, 540, getWidth(), 540, paint);
        canvas.drawBitmap(two, 0 + move, 420, null);
        if (supersize >= 201) {
            if (rox < 150) {
                rox += 1.5;
                canvas.drawBitmap(one_sha, -400 + move + supersize, 420 - rox, null);
            } else {
                rox = 150;
                canvas.drawBitmap(one_sha2, -400 + move + supersize, 420 - rox, null);
            }
        } else {
            canvas.drawBitmap(one, -400 + move + supersize, 420, null);
        }
        canvas.drawBitmap(three, tou1 - ko, 290, null);
        canvas.drawBitmap(three, tou2 - ko, 440, null);

        if (supersize >= 350 && supersize <= 500) {
            paint.setColor(kit1);
            canvas.drawCircle(70 + move, 425, 5, paint);
            canvas.drawCircle(70 + move, 425, 5 + size, paint);

        }
        if (supersize >= 195 && supersize <= 225) {
            paint.setColor(kit2);
            canvas.drawCircle(10 + move, 455, 5, paint);
            canvas.drawCircle(10 + move, 455, 5 + size, paint);
        }
    }

    int tou1 = 1500, tou2 = 1200;
    int ko;

    class JanThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(45);
                    if (ko <= 1700) {
                        ko += 2;
                    } else {
                        ko = 0;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    float rox;

    int num;
    int move;
    int supersize;
    boolean state = false;

    class MoveThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(45);
                    if (state == false) {
                        if (move >= 700) {
                            if (supersize < 1200) {
                                if (supersize == 198 || supersize == 535) {
                                    Thread.sleep(3000);
                                }
                                supersize += 3;
                            } else {
                                supersize = 1200;
                                state = true;
                            }
                        } else {
                            move += 2;

                        }
                    } else {
                        if (move >= 1300) {
                            move = 0;
                            supersize = 0;
                            rox = 0;
                            state = false;
                        } else {
                            move += 2;

                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class ShaderThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(45);
                    if (num >= 150000) {
                        num = 0;
                    } else {
                        num += 2;

                    }
                    handler.sendEmptyMessage(0x123);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    int kit1, kit2;
    boolean isState = true;
    int size;

    class SizeChange extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(70);
                    if (isState) {
                        if (size <= 10) {
                            size++;
                        } else {
                            isState = false;
                        }
                    } else {
                        if (size >= 0) {
                            size--;
                        } else {
                            isState = true;
                        }
                    }


                    handler.sendEmptyMessage(0x123);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
